package com.cares.event.listener.servicerun;

import java.util.List;
import java.util.TimerTask;

import javax.servlet.ServletContext;

import com.cares.event.constant.EventSystemConstant;
import com.cares.event.dto.EventActivityDto;
import com.cares.event.engine.EventFlowActivity;

public class EventTask extends TimerTask {

	private static boolean isRunning = false;   
    private ServletContext context = null;   
 
    public EventTask(ServletContext context){  
        this.context = context ;  
    }  
    
   @Override  
   public void run() {  
 
       if (!isRunning){  
           System.out.println("开始执行每秒定时刷任务开始") ;  
           isRunning = true ;  
           context.log("开始执行每秒定时刷任务开始.") ;  
           //获取内存中可执行的任务
           List<EventActivityDto> dtos = EventSystemConstant.getEventActivityDtos();
           if(dtos.size()>0){
        	   //根据任务个数创建线程个数
               Thread[] events = new Thread[dtos.size()];
               for(int i=0; i<dtos.size(); i++){
            	   EventFlowActivity event = new EventFlowActivity(dtos.get(i));
            	   events[i] = event;
               } 
               //批量启动线程，执行任务
               for(Thread event : events){
            	   event.start();
               }
           }
           isRunning = false;   
           context.log("每秒定时刷任务执行结束");   
           System.out.println("每秒定时刷任务执行结束") ;  
       }else{  
           context.log("上一次任务执行还未结束");   
       }  
   }  
   
}
